package com.rbac.modules.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rbac.modules.system.entity.SysPermission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SysPermissionMapper extends BaseMapper<SysPermission> {
    List<SysPermission> selectByRoleId(@Param("roleId") Long roleId);
    
    List<SysPermission> selectByUserId(@Param("userId") Long userId);
    
    List<SysPermission> selectByParentId(@Param("parentId") Long parentId);

    @Select("SELECT DISTINCT p.permission_code FROM sys_permission p " +
            "INNER JOIN sys_role_permission rp ON p.id = rp.permission_id " +
            "INNER JOIN sys_user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.status = 1")
    List<String> selectPermissionCodesByUserId(Long userId);

    /**
     * 获取用户的所有权限编码
     */
    @Select("SELECT DISTINCT p.permission_code " +
            "FROM sys_permission p " +
            "INNER JOIN sys_role_permission rp ON p.id = rp.permission_id " +
            "INNER JOIN sys_user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.status = 1")
    List<String> getPermissionCodesByUserId(Long userId);

    /**
     * 获取用户的所有权限
     */
    @Select("SELECT DISTINCT p.* " +
            "FROM sys_permission p " +
            "INNER JOIN sys_role_permission rp ON p.id = rp.permission_id " +
            "INNER JOIN sys_user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.status = 1")
    List<SysPermission> getPermissionsByUserId(Long userId);
} 